<?php
namespace Api\Controller;
use Think\Controller;
class VideoController extends ApiBaseController{
	
	function callback(){
		\Think\Log::record(implode("", $_GET), 'INFO');
		\Think\Log::record(implode("", $_POST), 'INFO');
		
		$contents = file_get_contents('php://input');
		\Think\Log::record("腾讯云回调参数：".$contents, 'INFO');
		try {
			$data = json_decode($contents, true);
			$fileId = $data['data']['file_id'];
			$type = $_REQUEST['type'];   //类型 1为新闻视频 2为直播视频 3 直播资源池
			\Think\Log::record("获取到的types是".$type, 'INFO');
			$live_video_type = $_REQUEST['live_video_type'];  //如果为直播视频的话  1为未开始视频 2为回放视频
			
			if($type == '1'){
				//根据$fileId获取news_id
				$news_video_model = M('vrnews1.video','news_');
				$result_id = $news_video_model->where("file_id = '$fileId'")->getfield('news_id');
			}else if($type == '2'){
				//根据$fileId获取live_id
				$live_video_model = M('vrlive.video','live_');
				$live_video = $live_video_model->where("file_id = '$fileId'")->find();
				$result_id = $live_video['live_id'];
				$live_video_type = $live_video['type'];
			}else{
				//根据$fileId获取live_id
				//3 直播资源池
				$live_video_model = D("Common/LiveResourceVideo");
				$live_video = $live_video_model->where("file_id = '$fileId'")->find();
				$result_id = $live_video['video_id'];
			}
			$test_sql = M()->getLastSql();
			\Think\Log::record("查询file_id".$test_sql, 'INFO');
			\Think\Log::record("file_id查到的值是:".$result_id, 'INFO');
			if($result_id){
				$key = $_REQUEST['key'];
				$is_vr = $_REQUEST['is_vr'];
				$video_type = $_REQUEST['video_type'];
				if(sha1(md5(".$result_id.")+"_xhw") == $key){
					$imgUrls = $data['data']['image_video']['imgUrls'];
					$videoUrls = $data['data']['image_video']['videoUrls'];
					$thumbnailUrl = "";
					foreach ($imgUrls as $v){
						if(1 == $v['id'] && '640' == $v['vwidth']){
							$thumbnailUrl = $v['url'];
							break;
						}
					}
					$videoUrl = "";
					$height = "";
					$weight = "";
					$size = "";
					$add_data = array();
					foreach ($videoUrls as $v){
// 						if($is_vr == '1'){
// 							if(10 == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
// 								$videoUrl = $v['url'];
// 								$height = $v['vheight'];
// 								$weight = $v['vwidth'];
// 								$size = $v['size'];
// 							}
// 						}else{
// 							if(30 == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
// 								$videoUrl = $v['url'];
// 								$height = $v['vheight'];
// 								$weight = $v['vwidth'];
// 								$size = $v['size'];
// 							}
// 						}
// 						if(230 == $v['definition'] && strrchr($v['url'], "m3u8") == "m3u8"){
// 							$videoUrl = $v['url'];
// 							$height = $v['vheight'];
// 							$weight = $v['vwidth'];
// 							$size = $v['size'];
// 							break;
// 						}
// 						if($is_vr == '1'){
// 							if('0' == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
// 								$add_data['video_url'] = $v['url'];
// 								$add_data['height'] = $v['vheight'];
// 								$add_data['width'] = $v['vwidth'];
// 								$add_data['size'] = $v['size'];
// 							}
// 						}else{
							if(30 == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
								$add_data['video_url'] = $v['url'];
								$add_data['height'] = $v['vheight'];
								$add_data['width'] = $v['vwidth'];
								$add_data['size'] = $v['size'];
							}
// 						}
						if(20 == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
							$add_data['video_url1'] = $v['url'];
							$add_data['height1'] = $v['vheight'];
							$add_data['width1'] = $v['vwidth'];
							$add_data['size1'] = $v['size'];
						}
						if(10 == $v['definition'] && strrchr($v['url'], "mp4") == "mp4"){
							$add_data['video_url2'] = $v['url'];
							$add_data['height2'] = $v['vheight'];
							$add_data['width2'] = $v['vwidth'];
							$add_data['size2'] = $v['size'];
						}
					}
					
				}
				if(empty($add_data['video_url'])){
					$add_data['video_url'] = $add_data['video_url1'];
				}
				//时长
				$duration = $this->changeTimeType($data['data']['image_video']['duration']);
				//更新视频表
				$add_data['thumbnail_url'] = $thumbnailUrl;
				$add_data['duration'] = $duration;
				//is_vr 1 是vr 0不是  category 1 普通视频 2 vr视频
				$add_data['category'] = $is_vr == 1 ? 2 : 1;
				$add_data['update_time'] = date('Y-m-d H:i:s');
				$add_data['text'] = $contents;
				$add_data['status'] = '2';
				if($type == '2'){
					$live_video_model->where("file_id = '$fileId'")->data($add_data)->save();
					$live = D("Common/Live");
					if($live_video_type == 1){
						$live->where(array('live_id'=>$result_id))->save(array('befor_start_url'=>$add_data['video_url']));
					}else{
						$live->where(array('live_id'=>$result_id))->save(array('rever_url'=>$add_data['video_url']));
					}
				}else if($type == '3'){
					//直播资源池视频
					$live_video_model->where("video_id = $result_id")->data($add_data)->save();
				}else{
					$news_video_model->where("file_id = '$fileId'")->data($add_data)->save();
				}
				$sql = M()->getLastSql();
				\Think\Log::record("最后更新的sql是:".$sql, 'INFO');
			}
		}catch (Exception $e){
			\Think\Log::record($e->getMessage(), 'WARN');
			\Think\Log::record($e->getTraceAsString(), 'WARN');
		}
		exit();
	}
	
	
	/**
	 * 直播码直播回调
	 * @cong.zhao
	 * @2017-08-07
	 */
	function codelive_callback(){
		$contents = file_get_contents('php://input');	
		try {
			\Think\Log::record($contents, 'INFO');
			$data = json_decode($contents, true);
			$channel_id = isset($data['channel_id']) ? $data['channel_id'] : '';
			$file_id = isset($data['file_id']) ? $data['file_id'] : '';
			$file_format = isset($data['file_format']) ? $data['file_format'] : '';
			$video_url = isset($data['video_url']) ? $data['video_url'] : '';
			$event_type = isset($data['event_type']) ? $data['event_type'] : '';
					
			if($channel_id && $event_type == '100'){
								
				$live_channel_model = M('vrlive.channel','live_');
				$source_id = $live_channel_model->where("txy_channel_id = '$channel_id'")->getfield('channel_id');
				
				$camera_angle_model = M('vrlive.camera_angle','live_');
				$camera_id = $camera_angle_model->where("source_id = '$source_id'")->getfield('camera_id');
				
				$concat_file_id = $camera_angle_model->where("source_id = '$source_id'")->getfield('concat_file_id');
				$live_id = $camera_angle_model->where("source_id = '$source_id'")->getfield('live_id');
				
				if($camera_id){
					$code_files_model = M('vrlive.code_files','live_');
					$add_data = array();
					$add_data['code_camera_id'] = $camera_id;
					$add_data['file_id'] = isset($data['file_id']) ? $data['file_id'] : '';
					$add_data['file_size'] = isset($data['file_size']) ? $data['file_size'] : '';
					$add_data['video_url'] = isset($data['video_url']) ? $data['video_url'] : '';
					$add_data['duration'] = isset($data['duration']) ? $data['duration'] : '';
					$add_data['file_format'] = isset($data['file_format']) ? $data['file_format'] : '';
					if($code_files_model->add($add_data)){
						//如果存在上一段录制文件，则进行合并
						if($file_format == 'mp4'){
							if($concat_file_id){								
								//创建拼接任务
								$config = array('SecretId'       => C("CODE_API_SecretId"),
										'SecretKey'      => C("CODE_API_SecretKey"),
										'RequestMethod'  => 'GET',
										'DefaultRegion'  => C("API_DefaultRegion"));
								vendor("QcloudApi.QcloudApi");
								$service = \QcloudApi::load(\QcloudApi::MODULE_VIDEO, $config);
								//要输入的参数
								$time = time();
								$package = array(
										'srcFileList.0.fileId'=>$concat_file_id,
										'srcFileList.1.fileId'=>$file_id,
										'name'=>$time.'filename',
										'dstType.0'=>'mp4',
								);
								
								$result = $service->ConcatVideo($package);
								if($result['codeDesc'] == 'Success'){
									//更新对应拼接任务成功之后的任务id
									$camera_angle_model->where("source_id = '$source_id'")->data(array('concat_task_id'=>$result['vodTaskId']))->save();
								}
							}else{							
								$camera_angle_model->where("source_id = '$source_id'")->data(array('concat_file_id'=>$file_id))->save();
								//更新直播的回放地址
								$live_data = array();
								$live_video_model = M('vrlive.live');
								$live_status = $live_video_model->where("live_id = $live_id")->getfield('status');
								if($live_status == '2') $live_data['status'] = '5';
								$live_data['rever_url'] = $video_url;
								$live_video_model->where("live_id = $live_id")->data($live_data)->save();
							}
							
						}
					}
				}
				
			}
			
	
		}catch (Exception $e){
			\Think\Log::record($e->getMessage(), 'WARN');
			\Think\Log::record($e->getTraceAsString(), 'WARN');
		}
		exit();
	}
	
	
	
	/**
	 * 视频拼接回调
	 * @cong.zhao
	 * @2017-08-07
	 */
	function concat_callback(){
		$contents = file_get_contents('php://input');		
		try {
			$data = json_decode($contents, true);
			$vodTaskId = $data['data']['vodTaskId'];
			$fileInfo = $data['data']['fileInfo'][0];			
			
			//根据返回的拼接视频任务id查询对应直播id
			$camera_angle_model = M('vrlive.camera_angle','live_');
			$live_id = $camera_angle_model->where("concat_task_id = '$vodTaskId'")->getfield('live_id');
			
			//更新直播的回放地址
			if($live_id){
				$live_video_model = M('vrlive.live');
				if($fileInfo['status'] == '0'){
					//更新对应拼接任务成功之后的新文件的file_id
					$camera_angle_model->where("concat_task_id = '$vodTaskId'")->data(array('concat_file_id'=>$fileInfo['fileId']))->save();
					
					$live_data = array();
					$live_status = $live_video_model->where("live_id = $live_id")->getfield('status');
					if($live_status == '2') $live_data['status'] = '5';
					
					//更新直播的回放地址
					$live_data['rever_url'] = $fileInfo['fileUrl'];
					$live_video_model->where("live_id = $live_id")->data($live_data)->save();
				}	
			}

		}catch (Exception $e){
			\Think\Log::record($e->getMessage(), 'WARN');
			\Think\Log::record($e->getTraceAsString(), 'WARN');
		}
		exit();
	}
	
	
	function changeTimeType($seconds) { 
	    if ($seconds > 3600) { 
	        $hours = intval($seconds / 3600); 
	        $minutes = $seconds % 3600; 
	        $time = $hours . ":" . gmstrftime('%M:%S', $minutes); 
	    } else { 
	        $time = gmstrftime('%H:%M:%S', $seconds); 
	    } 
	    return $time; 
	}
	
	
	function get_video(){
		$config = array('SecretId'       => C("API_SecretId"),
				'SecretKey'      => C("API_SecretKey"),
				'RequestMethod'  => 'GET',
				'DefaultRegion'  => C("API_DefaultRegion"));
		vendor("QcloudApi.QcloudApi");
		$service = \QcloudApi::load(\QcloudApi::MODULE_VIDEO, $config);
		//要输入的参数
		$package = array(
				'fileId' => '14651978969340560036'
		);
		$a = $service->DescribeVodPlayUrls($package);
		\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
		print $service->getLastRequest();
		print_r($a);
	}
	
}